Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

936
Visualizações
Error [ERR_REQUIRE_ESM]: require() of ES Module while using SSR (Inertiajs)

Here's the stack I'm using: Laravel, Inertiajs, React

I was setting up Inertia SSR, everything got installed and compiled successfully. But just while running the final node process ( node public/js/ssr.js), I'm getting this error:

Error [ERR_REQUIRE_ESM]: require() of ES Module E:\Websites\laravel\...\node_modules\swiper\swiper.esm.js from E:\Websites\laravel\...\public\js\ssr.js not supported.
Instead change the require of swiper.esm.js in E:\Websites\laravel\...\public\js\ssr.js to a dynamic import() which is available in all CommonJS modules.

However, there's no error on the client-end. Why the same package have no issue on the client-end but just only on the server-end.

I want to use the latest Swiper version.

Here's my laravel-mix config

// webpack.mix.js
mix.js('resources/js/app.js', 'public/js')
    .react()
    .postCss('resources/css/app.css', 'public/css', [
        require('postcss-import'),
        require('tailwindcss'),
        require('autoprefixer'),
    ])
    .alias({
        '@': 'resources/js',
    })
    .version();
// webpack.ssr.mix.js
mix.js('resources/js/ssr.js', 'public/js')
  .options({ manifest: false })
  .react()
  .alias({ '@': path.resolve('resources/js') })
  .alias({ ziggy: path.resolve('vendor/tightenco/ziggy/src/js') })
  .webpackConfig({
    target: 'node',
    externals: [nodeExternals()],
  })
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

If you look at the swiper NPM package, then it is published as ESM only module. It is not not a Common.js module. Look at the following package.json fields of swiper package:

"type": "module",
"main": "./swiper.esm.js",
"module": "./swiper.esm.js",

The reason why it works on client side is that - you bundle everything using Webpack. The Webpack then takes care of bundling AMD, Common.js, UMD and ES Module into a single file with appropriate IIFE wrapper. And thus, it never gives the error.

The story on server side is bit different. In Node.js, you cannot import ESM module using require() function. And during compilation, all your import statement get converted into require() calls since your target is node for server-side code. Also, as you are using webpack-node-externals to not bundle the third-module available via node_modules, Webpack will not bundle this package into your server build. You can tell Webpack to ignore all modules except swiper by adding it to allow list.

nodeExternals({
  allowlist: [
    'swiper'
  ]
})

This will tell Webpack to bundle swiper into the server build and you will no longer see this error. Note that, you may have to do this process for each ESM only module that you use.

You can also try the alternative of using custom webpack resolver for swiper package, but since you are already using webpack-node-externals, it is simpler way.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda